import { observable, action, toJS } from 'mobx';
import { Bubble, Tools } from 'weaver-mobile';
import { RouteLayout, ListViewPage, SearchPage } from 'weaver-mobile-page';
import * as Api from '../apis';

const getLabel = Tools.getLabel;
const initState = {
  voteListLoading: true,
  shareVisible: false,
  shareChoose: false,
  isSearch: false,
  groupid: '',
  isgroup: '',
  listSearchName: '',
}
const template = { default: [{ key: 'col', configs: [{ key: 'col_row', configs: [{ key: 'name' }] }] }] };
export default class ListStore {
  @observable newVoteLayout = new RouteLayout.Store({ route: '/groupVote', id: 'new'});
  @observable votingLayout = new RouteLayout.Store({ route: '/groupVote', id: 'voting'});
  @observable votingNewLayout = new RouteLayout.Store({ route: '/groupVote/new', id: 'vote'});
  @observable votedLayout = new RouteLayout.Store({ route: '/groupVote', id: 'voted'});
  @observable voteList = new ListViewPage.Store({ route: '/groupVote', id: 'list' });
  @observable searchPage = new SearchPage.Store({ route: '/groupVote', id: 'searchhistory', template })

  @observable voteListLoading = initState.voteListLoading;
  @observable shareVisible = initState.shareVisible; // popver 显隐
  @observable shareChoose = initState.shareChoose; // 批量分享
  @observable isSearch = initState.isSearch; 
  @observable groupid = initState.groupid;
  @observable isgroup = initState.isgroup; // 值为0则表示单聊，值为1则表示为群聊
  @observable listSearchName = initState.listSearchName;  // 列表搜索条件存储

  @action setStoreState = (params) => {
    Object.keys(params).forEach(key => {
      this[key] = params[key];
    })
  }

  /* 列表请求 */
  @action getGroupVoteList = (params) => {
    params = {
      groupid: this.groupid,
      isgroup: this.isgroup, // 1：是， 0：否
      ...params
    }
    if (this.listSearchName && this.isSearch) {
      params = {...params, groupvotename: this.listSearchName}
    }
    Api.getGroupVoteList(params).then(res => {
      const dataKey = res.sessionkey;
      this.voteList.getData({ dataKey }, () => this.voteListLoading = false, true)
    })
  }

  // 取消批量
  @action doShareCancel = () => {
    this.shareChoose = false;
    this.doShareSelectNone();
  }

  // 全选
  @action doShareSelectAll = () => this.voteList.selectAll();

  // 取消全选
  @action doShareSelectNone = () => this.voteList.selectNone();

  // 批量分享
  @action groupchatvotealert = (params, callback) => {
    Api.groupchatvotealert(params).then(res => {
      if (res.api_status === '1') {
        this.doShareCancel();
        if (callback && typeof callback === 'function') callback(res);
        Bubble.success(getLabel('391211','分享成功！'));
      } else {
        Bubble.error(getLabel('516187','分享失败，请重试！'));
      }
    })
  }

  // 删除群投票
  @action groupchatvotedel = (params) => {
    Api.groupchatvotedel(params).then(res => {
      switch(res.api_status) {
        case 'success': 
          this.getGroupVoteList();
          Bubble.success(getLabel('83472','删除成功!'));
          break;
        case '0':
          Bubble.error(getLabel('516185','暂无权限删除！'))
          break;
        case '2':
          Bubble.error(getLabel('516186','该投票已删除！'))
          break;
        default:
          Bubble.error(getLabel('384401','删除失败，请重试！'));
      }
    })
  }

  updateListData = (ids, params) => {
    const { data, updateDataSource, setState } = this.voteList;
    let curData = [];
    curData = toJS(data).map(v => {
      if (ids.split(',').indexOf(v.id) > -1) {
        return {
          ...v,
          ...params,
        }
      }
      return v;
    });
    Promise.all([setState({ data: curData})]).then(action(() => {
      setState({ selectedRowKeys: [] });
      updateDataSource();
    }));
  }
}